import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
	state: {
		cancelFns: {},
	},
	mutations: {
		// 用于收集发送出去的请求以及取消请求的cb函数
		ADDCANCELFN(state, { url, cb }) {
			state.cancelFns[url] = cb;
		},
		// 删除已经已经响应的请求url和cb函数
		DELETECANCELFN(state, url) {
			delete state.cancelFns[url];
		},
		// 执行所有未删除的cb函数
		CLEARCANCELFNS(state) {
			Object.values(state.cancelFns).forEach((cb) => {
				cb();
			});
			// 清空state.cancelFns
			state.cancelFns = {};
		},
	},
});
